package com.isis.service;

import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailService {
	
	public int sendMail(String emailUser,String admin,String subject,String msgBody){
		int rep = 2;
		Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

       if(msgBody==null){
    	   msgBody="";
       }
       if(subject==null){
    	   subject="";
       }
        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(emailUser, "User of isisprojetchat"));
            msg.addRecipient(Message.RecipientType.TO,new InternetAddress(admin, "message on isisprojetchat.appspot.com"));
            msg.setSubject(subject);
            msg.setText(msgBody);
            Transport.send(msg);
            rep=1;
            
    
        } catch (AddressException e) {
        	e.printStackTrace();
        	return 2;
        } catch (MessagingException e) {
        	e.printStackTrace();
        	return 2;
        } catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 2;
		}
		
		return rep;				
	}

}
